package models

import (
	"errors"
	"github.com/beego/beego/v2/adapter/orm"
)

type Users struct {
	Id        int32
	Uuid      string
	Username  string
	Password  string
	Email     string
	Nickname  string
	Avatar    string
	Salt      string
	CreatedAt string
}

func (u *Users) TableName() string {
	return "ws_users"
}

func init() {
	orm.RegisterModel(new(Users))
}

func db() (o orm.Ormer) {
	o = orm.NewOrm()
	o.Using("default")
	return o
}

func FindUser(uid int32) (u *Users, err error) {
	var user = new(Users)
	user.Id = uid
	if err := db().Read(user, "id"); err == nil {
		return user, nil
	}
	return nil, errors.New("用户信息不存在")
}

func DeleteUser(uid int32) (i int64, err error) {
	i, err = db().Delete(&Users{Id: uid})
	if err != nil {
		return i, err
	} else {
		return i, nil
	}
}
